home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Bank smakow / BankSmakow.air / BankSmakow.swf / scripts / mx / controls / treeClasses / HierarchicalViewCursor.as < prev    next >
Text File  |  2009-12-16  |  19KB  |  655 lines

  1. package mx.controls.treeClasses
  2. {
  3.    import flash.events.EventDispatcher;
  4.    import flash.utils.Dictionary;
  5.    import mx.collections.CursorBookmark;
  6.    import mx.collections.ICollectionView;
  7.    import mx.collections.IViewCursor;
  8.    import mx.core.mx_internal;
  9.    import mx.events.CollectionEvent;
  10.    import mx.events.CollectionEventKind;
  11.    
  12.    use namespace mx_internal;
  13.    
  14.    public class HierarchicalViewCursor extends EventDispatcher implements IViewCursor
  15.    {
  16.       
  17.       mx_internal static const VERSION:String = "3.5.0.12683";
  18.        
  19.       
  20.       private var itemToUID:Function;
  21.       
  22.       private var openNodes:Object;
  23.       
  24.       private var collection:HierarchicalCollectionView;
  25.       
  26.       private var childIndexStack:Array;
  27.       
  28.       private var parentNodes:Array;
  29.       
  30.       private var _currentDepth:int = 1;
  31.       
  32.       private var model:ICollectionView;
  33.       
  34.       private var dataDescriptor:ITreeDataDescriptor;
  35.       
  36.       private var more:Boolean;
  37.       
  38.       private var currentIndex:int = 0;
  39.       
  40.       private var modelCursor:IViewCursor;
  41.       
  42.       private var childNodes:Object;
  43.       
  44.       private var currentChildIndex:int = 0;
  45.       
  46.       public function HierarchicalViewCursor(param1:HierarchicalCollectionView, param2:ICollectionView, param3:ITreeDataDescriptor, param4:Function, param5:Object)
  47.       {
  48.          childNodes = [];
  49.          parentNodes = [];
  50.          childIndexStack = [];
  51.          super();
  52.          this.collection = param1;
  53.          this.model = param2;
  54.          this.dataDescriptor = param3;
  55.          this.itemToUID = param4;
  56.          this.openNodes = param5;
  57.          param1.addEventListener(CollectionEvent.COLLECTION_CHANGE,collectionChangeHandler,false,0,true);
  58.          modelCursor = param2.createCursor();
  59.          if(param2.length > 1)
  60.          {
  61.             more = true;
  62.          }
  63.          else
  64.          {
  65.             more = false;
  66.          }
  67.       }
  68.       
  69.       private function isNodeBefore(param1:Object, param2:Object) : Boolean
  70.       {
  71.          var _loc3_:int = 0;
  72.          var _loc4_:int = 0;
  73.          var _loc5_:ICollectionView = null;
  74.          var _loc6_:Object = null;
  75.          var _loc9_:Object = null;
  76.          var _loc10_:Object = null;
  77.          var _loc11_:Object = null;
  78.          if(param2 == null)
  79.          {
  80.             return false;
  81.          }
  82.          var _loc7_:Array = getParentStack(param1);
  83.          var _loc8_:Array = getParentStack(param2);
  84.          while(_loc7_.length && _loc8_.length)
  85.          {
  86.             _loc9_ = _loc7_.shift();
  87.             _loc10_ = _loc8_.shift();
  88.             if(_loc9_ != _loc10_)
  89.             {
  90.                if((_loc6_ = collection.getParentItem(_loc9_)) != null && dataDescriptor.isBranch(_loc6_,model) && dataDescriptor.hasChildren(_loc6_,model))
  91.                {
  92.                   _loc5_ = dataDescriptor.getChildren(_loc6_,model);
  93.                }
  94.                else
  95.                {
  96.                   _loc5_ = model;
  97.                }
  98.                _loc3_ = _loc5_.length;
  99.                if((_loc11_ = _loc5_[_loc4_]) == _loc10_)
  100.                {
  101.                   return false;
  102.                }
  103.                if(_loc11_ == _loc9_)
  104.                {
  105.                   return true;
  106.                }
  107.             }
  108.          }
  109.          if(_loc7_.length)
  110.          {
  111.             param1 = _loc7_.shift();
  112.          }
  113.          if(_loc8_.length)
  114.          {
  115.             param2 = _loc8_.shift();
  116.          }
  117.          _loc3_ = (_loc5_ = model).length;
  118.          _loc4_ = 0;
  119.          while(_loc4_ < _loc3_)
  120.          {
  121.             if((_loc11_ = _loc5_[_loc4_]) == param2)
  122.             {
  123.                return false;
  124.             }
  125.             if(_loc11_ == param1)
  126.             {
  127.                return true;
  128.             }
  129.             _loc4_++;
  130.          }
  131.          return false;
  132.       }
  133.       
  134.       public function findFirst(param1:Object) : Boolean
  135.       {
  136.          return findAny(param1);
  137.       }
  138.       
  139.       public function remove() : Object
  140.       {
  141.          return null;
  142.       }
  143.       
  144.       public function get afterLast() : Boolean
  145.       {
  146.          return currentIndex >= collection.length && current == null;
  147.       }
  148.       
  149.       public function collectionChangeHandler(param1:CollectionEvent) : void
  150.       {
  151.          var _loc2_:int = 0;
  152.          var _loc3_:int = 0;
  153.          var _loc4_:Object = null;
  154.          var _loc5_:Object = null;
  155.          var _loc6_:Object = null;
  156.          var _loc7_:Array = null;
  157.          var _loc8_:Dictionary = null;
  158.          var _loc10_:int = 0;
  159.          var _loc9_:Boolean = false;
  160.          _loc7_ = getParentStack(current);
  161.          _loc8_ = new Dictionary();
  162.          _loc3_ = _loc7_.length;
  163.          _loc2_ = 0;
  164.          while(_loc2_ < _loc3_ - 1)
  165.          {
  166.             _loc8_[_loc7_[_loc2_]] = _loc2_ + 1;
  167.             _loc2_++;
  168.          }
  169.          _loc6_ = _loc7_[_loc7_.length - 1];
  170.          if(param1.kind == CollectionEventKind.ADD)
  171.          {
  172.             _loc3_ = param1.items.length;
  173.             if(param1.location <= currentIndex)
  174.             {
  175.                currentIndex += _loc3_;
  176.                _loc9_ = true;
  177.             }
  178.             _loc2_ = 0;
  179.             while(_loc2_ < _loc3_)
  180.             {
  181.                _loc4_ = param1.items[_loc2_];
  182.                if(_loc9_)
  183.                {
  184.                   if((_loc5_ = collection.getParentItem(_loc4_)) == _loc6_)
  185.                   {
  186.                      ++currentChildIndex;
  187.                   }
  188.                   else if(_loc8_[_loc5_] != null)
  189.                   {
  190.                      ++childIndexStack[_loc8_[_loc5_]];
  191.                   }
  192.                }
  193.                _loc2_++;
  194.             }
  195.          }
  196.          else if(param1.kind == CollectionEventKind.REMOVE)
  197.          {
  198.             _loc3_ = param1.items.length;
  199.             if(param1.location <= currentIndex)
  200.             {
  201.                if(param1.location + _loc3_ >= currentIndex)
  202.                {
  203.                   _loc10_ = param1.location;
  204.                   moveToFirst();
  205.                   seek(CursorBookmark.FIRST,_loc10_);
  206.                   _loc2_ = 0;
  207.                   while(_loc2_ < _loc3_)
  208.                   {
  209.                      _loc4_ = param1.items[_loc2_];
  210.                      delete collection.parentMap[itemToUID(_loc4_)];
  211.                      _loc2_++;
  212.                   }
  213.                   return;
  214.                }
  215.                currentIndex -= _loc3_;
  216.                _loc9_ = true;
  217.             }
  218.             _loc2_ = 0;
  219.             while(_loc2_ < _loc3_)
  220.             {
  221.                _loc4_ = param1.items[_loc2_];
  222.                if(_loc9_)
  223.                {
  224.                   if((_loc5_ = collection.getParentItem(_loc4_)) == _loc6_)
  225.                   {
  226.                      --currentChildIndex;
  227.                   }
  228.                   else if(_loc8_[_loc5_] != null)
  229.                   {
  230.                      --childIndexStack[_loc8_[_loc5_]];
  231.                   }
  232.                }
  233.                delete collection.parentMap[itemToUID(_loc4_)];
  234.                _loc2_++;
  235.             }
  236.          }
  237.       }
  238.       
  239.       public function get view() : ICollectionView
  240.       {
  241.          return model;
  242.       }
  243.       
  244.       public function get bookmark() : CursorBookmark
  245.       {
  246.          return new CursorBookmark(currentIndex.toString());
  247.       }
  248.       
  249.       public function insert(param1:Object) : void
  250.       {
  251.       }
  252.       
  253.       private function getParentStack(param1:Object) : Array
  254.       {
  255.          var _loc2_:Array = [];
  256.          var _loc3_:Object = collection.getParentItem(param1);
  257.          while(_loc3_ != null)
  258.          {
  259.             _loc2_.unshift(_loc3_);
  260.             _loc3_ = collection.getParentItem(_loc3_);
  261.          }
  262.          return _loc2_;
  263.       }
  264.       
  265.       public function get currentDepth() : int
  266.       {
  267.          return _currentDepth;
  268.       }
  269.       
  270.       private function isItemVisible(param1:Object) : Boolean
  271.       {
  272.          var _loc2_:Object = collection.getParentItem(param1);
  273.          while(_loc2_ != null)
  274.          {
  275.             if(openNodes[itemToUID(_loc2_)] == null)
  276.             {
  277.                return false;
  278.             }
  279.             _loc2_ = collection.getParentItem(_loc2_);
  280.          }
  281.          return true;
  282.       }
  283.       
  284.       public function moveToLast() : void
  285.       {
  286.          var _loc3_:Object = null;
  287.          childNodes = [];
  288.          childIndexStack = [];
  289.          _currentDepth = 1;
  290.          parentNodes = [];
  291.          var _loc1_:Boolean = false;
  292.          modelCursor.seek(CursorBookmark.LAST,0);
  293.          var _loc2_:Object = modelCursor.current;
  294.          while(openNodes[itemToUID(_loc2_)] && dataDescriptor.isBranch(_loc2_,model) && dataDescriptor.hasChildren(_loc2_,model))
  295.          {
  296.             _loc3_ = childNodes;
  297.             childNodes = dataDescriptor.getChildren(_loc2_,model);
  298.             if(!(childNodes != null && childNodes.length > 0))
  299.             {
  300.                childNodes = _loc3_;
  301.                break;
  302.             }
  303.             parentNodes.push(_loc2_);
  304.             childIndexStack.push(currentChildIndex);
  305.             _loc2_ = childNodes[childNodes.length - 1];
  306.             currentChildIndex = childNodes.length - 1;
  307.             ++_currentDepth;
  308.          }
  309.          currentIndex = collection.length - 1;
  310.       }
  311.       
  312.       public function movePrevious() : Boolean
  313.       {
  314.          var grandParent:Object = null;
  315.          var previousChildNodes:Object = null;
  316.          var currentNode:Object = current;
  317.          if(currentNode == null)
  318.          {
  319.             return false;
  320.          }
  321.          if(parentNodes && parentNodes.length > 0)
  322.          {
  323.             if(currentChildIndex == 0)
  324.             {
  325.                currentNode = parentNodes.pop();
  326.                currentChildIndex = childIndexStack.pop();
  327.                grandParent = parentNodes[parentNodes.length - 1];
  328.                if(grandParent != null && dataDescriptor.isBranch(grandParent,model) && dataDescriptor.hasChildren(grandParent,model))
  329.                {
  330.                   childNodes = dataDescriptor.getChildren(grandParent,model);
  331.                }
  332.                else
  333.                {
  334.                   childNodes = [];
  335.                }
  336.                --_currentDepth;
  337.                --currentIndex;
  338.                return true;
  339.             }
  340.             try
  341.             {
  342.                currentNode = childNodes[--currentChildIndex];
  343.             }
  344.             catch(e:RangeError)
  345.             {
  346.                return false;
  347.             }
  348.          }
  349.          else
  350.          {
  351.             more = modelCursor.movePrevious();
  352.             if(!more)
  353.             {
  354.                currentIndex = -1;
  355.                currentNode = null;
  356.                return false;
  357.             }
  358.             currentNode = modelCursor.current;
  359.          }
  360.          while(openNodes[itemToUID(currentNode)] && dataDescriptor.isBranch(currentNode,model) && dataDescriptor.hasChildren(currentNode,model))
  361.          {
  362.             previousChildNodes = childNodes;
  363.             childNodes = dataDescriptor.getChildren(currentNode,model);
  364.             if(childNodes.length <= 0)
  365.             {
  366.                childNodes = previousChildNodes;
  367.                break;
  368.             }
  369.             childIndexStack.push(currentChildIndex);
  370.             parentNodes.push(currentNode);
  371.             currentChildIndex = childNodes.length - 1;
  372.             currentNode = childNodes[currentChildIndex];
  373.             ++_currentDepth;
  374.          }
  375.          --currentIndex;
  376.          return true;
  377.       }
  378.       
  379.       public function moveNext() : Boolean
  380.       {
  381.          var previousChildNodes:Object = null;
  382.          var grandParent:Object = null;
  383.          var currentNode:Object = current;
  384.          if(currentNode == null)
  385.          {
  386.             return false;
  387.          }
  388.          var uid:String = itemToUID(currentNode);
  389.          if(!collection.parentMap.hasOwnProperty(uid))
  390.          {
  391.             collection.parentMap[uid] = parentNodes[parentNodes.length - 1];
  392.          }
  393.          if(openNodes[uid] && dataDescriptor.isBranch(currentNode,model) && dataDescriptor.hasChildren(currentNode,model))
  394.          {
  395.             previousChildNodes = childNodes;
  396.             childNodes = dataDescriptor.getChildren(currentNode,model);
  397.             if(childNodes.length > 0)
  398.             {
  399.                childIndexStack.push(currentChildIndex);
  400.                parentNodes.push(currentNode);
  401.                currentChildIndex = 0;
  402.                currentNode = childNodes[0];
  403.                ++currentIndex;
  404.                ++_currentDepth;
  405.                return true;
  406.             }
  407.             childNodes = previousChildNodes;
  408.          }
  409.          while(childNodes != null && childNodes.length > 0 && currentChildIndex >= Math.max(childNodes.length - 1,0))
  410.          {
  411.             if(childIndexStack.length < 1 && !more)
  412.             {
  413.                currentNode = null;
  414.                ++currentIndex;
  415.                _currentDepth = 1;
  416.                return false;
  417.             }
  418.             currentNode = parentNodes.pop();
  419.             grandParent = parentNodes[parentNodes.length - 1];
  420.             if(grandParent != null && dataDescriptor.isBranch(grandParent,model) && dataDescriptor.hasChildren(grandParent,model))
  421.             {
  422.                childNodes = dataDescriptor.getChildren(grandParent,model);
  423.             }
  424.             else
  425.             {
  426.                childNodes = [];
  427.             }
  428.             currentChildIndex = childIndexStack.pop();
  429.             --_currentDepth;
  430.          }
  431.          if(childIndexStack.length == 0)
  432.          {
  433.             _currentDepth = 1;
  434.             more = modelCursor.moveNext();
  435.             if(!more)
  436.             {
  437.                _currentDepth = 1;
  438.                ++currentIndex;
  439.                currentNode = null;
  440.                return false;
  441.             }
  442.             currentNode = modelCursor.current;
  443.          }
  444.          else
  445.          {
  446.             try
  447.             {
  448.                currentNode = childNodes[++currentChildIndex];
  449.             }
  450.             catch(e:RangeError)
  451.             {
  452.                return false;
  453.             }
  454.          }
  455.          ++currentIndex;
  456.          return true;
  457.       }
  458.       
  459.       public function get index() : int
  460.       {
  461.          return currentIndex;
  462.       }
  463.       
  464.       public function findLast(param1:Object) : Boolean
  465.       {
  466.          var _loc3_:Object = null;
  467.          var _loc4_:Boolean = false;
  468.          var _loc5_:* = null;
  469.          seek(CursorBookmark.LAST);
  470.          var _loc2_:Boolean = false;
  471.          while(true)
  472.          {
  473.             if(_loc2_)
  474.             {
  475.                return false;
  476.             }
  477.             _loc3_ = current;
  478.             _loc4_ = true;
  479.             for(_loc5_ in param1)
  480.             {
  481.                if(_loc3_[_loc5_] != param1[_loc5_])
  482.                {
  483.                   _loc4_ = false;
  484.                   break;
  485.                }
  486.             }
  487.             if(_loc4_)
  488.             {
  489.                break;
  490.             }
  491.             _loc2_ = movePrevious();
  492.          }
  493.          return true;
  494.       }
  495.       
  496.       public function get beforeFirst() : Boolean
  497.       {
  498.          return currentIndex <= collection.length && current == null;
  499.       }
  500.       
  501.       public function get current() : Object
  502.       {
  503.          try
  504.          {
  505.             if(childIndexStack.length == 0)
  506.             {
  507.                return modelCursor.current;
  508.             }
  509.             return childNodes[currentChildIndex];
  510.          }
  511.          catch(e:RangeError)
  512.          {
  513.             return null;
  514.          }
  515.       }
  516.       
  517.       public function findAny(param1:Object) : Boolean
  518.       {
  519.          var _loc3_:Object = null;
  520.          var _loc4_:Boolean = false;
  521.          var _loc5_:* = null;
  522.          seek(CursorBookmark.FIRST);
  523.          var _loc2_:Boolean = false;
  524.          while(true)
  525.          {
  526.             if(_loc2_)
  527.             {
  528.                return false;
  529.             }
  530.             _loc3_ = dataDescriptor.getData(current);
  531.             _loc4_ = true;
  532.             for(_loc5_ in param1)
  533.             {
  534.                if(_loc3_[_loc5_] != param1[_loc5_])
  535.                {
  536.                   _loc4_ = false;
  537.                   break;
  538.                }
  539.             }
  540.             if(_loc4_)
  541.             {
  542.                break;
  543.             }
  544.             _loc2_ = moveNext();
  545.          }
  546.          return true;
  547.       }
  548.       
  549.       private function moveToFirst() : void
  550.       {
  551.          childNodes = [];
  552.          modelCursor.seek(CursorBookmark.FIRST,0);
  553.          if(model.length > 1)
  554.          {
  555.             more = true;
  556.          }
  557.          else
  558.          {
  559.             more = false;
  560.          }
  561.          currentChildIndex = 0;
  562.          parentNodes = [];
  563.          childIndexStack = [];
  564.          currentIndex = 0;
  565.          _currentDepth = 1;
  566.       }
  567.       
  568.       public function seek(param1:CursorBookmark, param2:int = 0, param3:int = 0) : void
  569.       {
  570.          var _loc4_:int = 0;
  571.          if(param1 == CursorBookmark.FIRST)
  572.          {
  573.             _loc4_ = 0;
  574.          }
  575.          else if(param1 == CursorBookmark.CURRENT)
  576.          {
  577.             _loc4_ = currentIndex;
  578.          }
  579.          else if(param1 == CursorBookmark.LAST)
  580.          {
  581.             _loc4_ = collection.length - 1;
  582.          }
  583.          else
  584.          {
  585.             _loc4_ = int(param1.value);
  586.          }
  587.          _loc4_ = Math.max(Math.min(_loc4_ + param2,collection.length),0);
  588.          var _loc5_:int = Math.abs(currentIndex - _loc4_);
  589.          var _loc6_:int = Math.abs(collection.length - _loc4_);
  590.          var _loc7_:* = true;
  591.          if(_loc5_ < _loc4_)
  592.          {
  593.             if(_loc6_ < _loc5_)
  594.             {
  595.                moveToLast();
  596.                if(_loc6_ == 0)
  597.                {
  598.                   moveNext();
  599.                   _loc4_ = 0;
  600.                }
  601.                else
  602.                {
  603.                   _loc4_ = _loc6_ - 1;
  604.                }
  605.                _loc7_ = false;
  606.             }
  607.             else
  608.             {
  609.                _loc7_ = currentIndex < _loc4_;
  610.                _loc4_ = _loc5_;
  611.                if(currentIndex == collection.length)
  612.                {
  613.                   _loc4_--;
  614.                   moveToLast();
  615.                }
  616.             }
  617.          }
  618.          else if(_loc6_ < _loc4_)
  619.          {
  620.             moveToLast();
  621.             if(_loc6_ == 0)
  622.             {
  623.                moveNext();
  624.                _loc4_ = 0;
  625.             }
  626.             else
  627.             {
  628.                _loc4_ = _loc6_ - 1;
  629.             }
  630.             _loc7_ = false;
  631.          }
  632.          else
  633.          {
  634.             moveToFirst();
  635.          }
  636.          if(_loc7_)
  637.          {
  638.             while(_loc4_ && _loc4_ > 0)
  639.             {
  640.                moveNext();
  641.                _loc4_--;
  642.             }
  643.          }
  644.          else
  645.          {
  646.             while(_loc4_ && _loc4_ > 0)
  647.             {
  648.                movePrevious();
  649.                _loc4_--;
  650.             }
  651.          }
  652.       }
  653.    }
  654. }
  655.